﻿//==============================================================================
// File: ValidationResultCollection.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.Collections.Generic;
using System.Linq;
using WpfRcp.PropertyModel.Collections;

namespace WpfRcp.PropertyModel.Validation
{
    /// <summary>
    /// Represents a collection of <see cref="ValidationResult"/> for both the
    /// whole object and per <see cref="MetaProperty"/>.
    /// </summary>
    public class ValidationResultCollection
    {
        private readonly IMultiMap<MetaProperty, ValidationResult> m_ValidationMap;
        private readonly IList<ValidationResult> m_ObjectValidationResults;

        /// <summary>
        /// Initializes the new instance of the <see cref="ValidationResultCollection"/> class.
        /// </summary>
        public ValidationResultCollection()
        {
            m_ValidationMap = new MultiMap<MetaProperty, ValidationResult>();
            m_ObjectValidationResults = new List<ValidationResult>();
        }

        /// <summary>
        /// Marks given property as valid.
        /// </summary>
        /// <param name="metaProperty"><see cref="MetaProperty"/> which will have all validation results cleaned.</param>
        public void ClearPropertyValidation(MetaProperty metaProperty)
        {
            if (m_ValidationMap.ContainsKey(metaProperty))
            {
                m_ValidationMap[metaProperty].Clear();
            }
        }

        /// <summary>
        /// Marks object as valid.
        /// </summary>
        public void ClearObjectValidation()
        {
            m_ObjectValidationResults.Clear();
        }

        /// <summary>
        /// Checks whether given property is valid.
        /// </summary>
        /// <param name="metaProperty"><see cref="MetaProperty"/> which is the subject of check.</param>
        /// <returns></returns>
        public bool CheckPropertyValidation(MetaProperty metaProperty)
        {
            if (m_ValidationMap.ContainsKey(metaProperty))
            {
                return m_ValidationMap[metaProperty].Count == 0;
            }

            return true;
        }

        /// <summary>
        /// Returns a collection of <see cref="ValidationResult"/>s for an object.
        /// </summary>
        /// <returns>A collection of <see cref="ValidationResult"/>s for an object.</returns>
        public IEnumerable<ValidationResult> GetValidationResults()
        {
            return m_ObjectValidationResults;
        }

        /// <summary>
        /// Returns a collection of <see cref="ValidationResult"/>s for a given property.
        /// </summary>
        /// <param name="metaProperty"><see cref="MetaProperty"/> for which
        /// <see cref="ValidationResult"/>s are being retrieved.</param>
        /// <returns>A collection of <see cref="ValidationResult"/>s for a given property.</returns>
        public IEnumerable<ValidationResult> GetValidationResults(MetaProperty metaProperty)
        {
            if (m_ValidationMap.ContainsKey(metaProperty))
            {
                return m_ValidationMap[metaProperty];
            }

            return Enumerable.Empty<ValidationResult>();
        }

        /// <summary>
        /// Adds a <see cref="ValidationResult"/> for an object.
        /// </summary>
        /// <param name="validationResult"><see cref="ValidationResult"/> to be added.</param>
        public void AddValidationResult(ValidationResult validationResult)
        {
            m_ObjectValidationResults.Add(validationResult);
        }

        /// <summary>
        /// Adds a <see cref="ValidationResult"/> for a given property.
        /// </summary>
        /// <param name="metaProperty"><see cref="MetaProperty"/> which identifies the property</param>
        /// <param name="validationResult"><see cref="ValidationResult"/> to be added.</param>
        public void AddValidationResult(MetaProperty metaProperty, ValidationResult validationResult)
        {
            m_ValidationMap.Add(metaProperty, validationResult);
        }
    }
}